package com.xindaoapp.happypet.ui;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Pair;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.bean.AddressDetailInfo;
import com.xindaoapp.happypet.bean.ExchangeResult;
import com.xindaoapp.happypet.bean.InteralGoodsdesc;
import com.xindaoapp.happypet.utils.CommonParameter.UserState;
import com.xindaoapp.happypet.utils.CommonUtil;
import com.xindaoapp.happypet.utils.IRequest;

/**
 * 确认兑换
 * 
 * @author Stone
 * 
 */
public class ChangeQueRenActivity extends BaseActivity {



    private TextView vNiuniu, mTitle, mJifen;
    private ImageView mImg;

    InteralGoodsdesc goods = null;
    private TextView mName, mNumber, mAddress;

    AddressDetailInfo addresss = null;

    String addressID = "";
    private TextView mIntegral;
    private RelativeLayout mRaddress;

    @Override
    protected int getContentViewResId() {
        return R.layout.fragment_changedescto;
    }

    @Override
    protected void initViews() {
        super.initViews();
        setTopBarTitle("确认兑换");
        vNiuniu = (TextView) findViewById(R.id.niuniubi);
        mName = (TextView) findViewById(R.id.name);
        mNumber = (TextView) findViewById(R.id.phone_number);
        mAddress = (TextView) findViewById(R.id.address);
        mRaddress = (RelativeLayout) findViewById(R.id.r_address);

        mImg = (ImageView) findViewById(R.id.img);
        mTitle = (TextView) findViewById(R.id.title);
        mJifen = (TextView) findViewById(R.id.jifen);

        mIntegral = (TextView) findViewById(R.id.tv_integral);
    }

    @Override
    protected void initEvents() {
        super.initEvents();

        mRaddress.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(ChangeQueRenActivity.this, ConsigneeNewActivity.class);
                // intent.putExtra(Constants.KEY_SKIP_CLASS_NAME,
                // ConsigneeIntegralFragment.class.getCanonicalName());
                startActivity(intent);
                // Bundle bundle = new Bundle();
                // this.startFragment(new ConsigneeFragment(), bundle);
            }
        });

        mIntegral.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if (TextUtils.isEmpty(addressID)) {
                    Toast.makeText(ChangeQueRenActivity.this, "未获取到收件人地址，请设置!!!", 0).show();
                } else {
                    goExchange(String.valueOf(goods.ex_id).trim(), addressID);
                }
            }


        });


    }


    /**
     * 最新的确认兑换按钮
     * 
     * @param ex_id
     * @param addressId
     */
    private void goExchange(String ex_id, String addressId) {
        // TODO Auto-generated method stub
        mIntegral.setEnabled(false);
        getMoccaApi().getExchangeResult(ex_id, addressId, new IRequest<ExchangeResult>() {

            @Override
            public void request(ExchangeResult t) {
                // TODO Auto-generated method stub
                mIntegral.setEnabled(true);
                if (null != t) {
                    if (t.result.equals("0")) {
                        Toast.makeText(ChangeQueRenActivity.this, t.msg, Toast.LENGTH_SHORT).show();
                        // 跳转到兑换列表界面

                        CommonUtil.refurshWeiba(getBaseContext());

                        Intent intent =
                                new Intent(ChangeQueRenActivity.this, NiuniuOrderActivity.class);
                        startActivityForResult(intent, 0);

                    } else {
                        Toast.makeText(ChangeQueRenActivity.this, t.msg, Toast.LENGTH_SHORT).show();
                    }
                } else {

                    try {
                        Toast.makeText(ChangeQueRenActivity.this, R.string.net_error,
                                Toast.LENGTH_SHORT).show();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

        });
    }



    // 确认兑换 --先扣牛牛币
    protected void goIntegral() {
        getMoccaApi().deleteIntegral(String.valueOf(goods.goods_id), goods.exchange_integral,
                new IRequest<Pair<Boolean, String>>() {

                    @Override
                    public void request(Pair<Boolean, String> t) {
                        if (null != t) {
                            if (t.first) {
                                // Toast.makeText(getActivity(), "扣钱成功~",
                                // Toast.LENGTH_SHORT).show();
                                addIntegralGoods();
                            } else {
                                Toast.makeText(ChangeQueRenActivity.this, t.second,
                                        Toast.LENGTH_LONG).show();
                            }
                        } else {

                            try {
                                Toast.makeText(ChangeQueRenActivity.this, R.string.net_error,
                                        Toast.LENGTH_SHORT).show();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
    }

    /**
     * 扣钱成功
     */
    protected void addIntegralGoods() {

        String uid = CommonUtil.decode(UserState.getUser().uid);
        getMoccaApi().addIntegralGoods(uid, String.valueOf(goods.ex_id), addressID,
                new IRequest<Pair<Boolean, String>>() {

                    @Override
                    public void request(Pair<Boolean, String> t) {
                        try {
                            if (null != t) {
                                if (t.first) {
                                    Toast.makeText(ChangeQueRenActivity.this, "兑换商品成功",
                                            Toast.LENGTH_SHORT).show();
                                    // Toast.makeText(getActivity(), t.second, Toast.LENGTH_SHORT)
                                    // .show();

                                    // Intent intent =
                                    // new Intent(ChangeQueRenActivity.this,
                                    // MainActivity.class);
                                    // intent.putExtra(Constants.KEY_SKIP_CLASS_NAME,
                                    // NiuniuOrderFragment.class.getCanonicalName());
                                    // startActivity(intent);
                                    // startFragment(new NiuniuOrderFragment());



                                    Intent intent =
                                            new Intent(ChangeQueRenActivity.this,
                                                    NiuniuOrderActivity.class);
                                    startActivityForResult(intent, 0);

                                } else {
                                    Toast.makeText(ChangeQueRenActivity.this, t.second,
                                            Toast.LENGTH_SHORT).show();
                                }
                            } else {
                                Toast.makeText(ChangeQueRenActivity.this, R.string.net_error,
                                        Toast.LENGTH_SHORT).show();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
    }


    @Override
    protected void onLoadDatas() {
        super.onLoadDatas();
        vNiuniu.setText(getIntent().getStringExtra("pro_jinbibi") + "尾巴币");// 设置牛牛币
        getMoccaApi().getDefaultAddress(UserState.getUserUid(), new IRequest<AddressDetailInfo>() {

            @Override
            public void request(AddressDetailInfo t) {
                // addresss = t;
                if (null != t) {
                    addressID = t.address_id;
                    mName.setText(t.username);
                    mNumber.setText(t.mobile);
                    mAddress.setText(t.address);
                } else {
                    mName.setText("未设置收货地址");
                    mNumber.setText("");
                    mAddress.setText("");
                }
            }

        });


        if (null != getIntent()) {
            String exid = getIntent().getStringExtra("ex_id");
            getMoccaApi().getChangeDesc(exid, new IRequest<InteralGoodsdesc>() {

                @Override
                public void request(InteralGoodsdesc t) {
                    if (t != null) {
                        goods = t;
                        ImageLoader.getInstance().displayImage(goods.goods_thumb, mImg);
                        mTitle.setText(goods.goods_name);
                        mJifen.setText(goods.exchange_integral + "尾巴币");
                    }
                }

            });
        }

    }

    @Override
    protected int getTopBarLeftImageViewRes() {
        return R.drawable.top_bar_back;
    }

    @Override
    protected OnClickListener getLeftViewClickListener() {
        return new OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        };
    }

    @Override
    protected int getTopBarRightTextViewRes() {
        return R.string.right_address;
    }

    @Override
    protected OnClickListener getRightViewClickListener() {
        return new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ChangeQueRenActivity.this, ConsigneeNewActivity.class);
                // intent.putExtra(Constants.KEY_SKIP_CLASS_NAME,
                // ConsigneeIntegralFragment.class.getCanonicalName());
                startActivity(intent);
            }
        };
    }

    @Override
    public void onResume() {
        super.onResume();
        onLoadDatas();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 1) {
            this.setResult(1);
            finish();
        }
    }
}
